#pragma once
#include <assert.h>
namespace lw
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin() { return _start; }
		iterator end() { return _finish; }
		const_iterator begin() const { return _start; }
		const_iterator end() const { return _finish; }

		vector() {}
		vector(size_t n, const T& val = T())
		{
			resize(n, val);
		}
		vector(int n, const T& val = T()) 
		{
			resize(n, val);
		}
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		vector(const std::initializer_list<T>& lt) 
		{
			reserve(lt.size());
			for (const auto& e : lt)
				push_back(e);
		}
		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (int i = 0; i < v.size(); i++)
				push_back(v[i]);
		}
		vector(vector<T>&& v)
		{
			swap(v);
		}
		vector<T>& operator=(const vector<T>& v) 
		{
			if (this != &v)
			{
				vector<T> tmp(v);
				swap(tmp);
			}
			return *this;
		}
		vector<T>& operator=(vector<T>&& v) 
		{
			swap(v);
			return *this;
		}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}
		size_t size() const { return _finish - _start; }
		size_t capacity() const { return _end_of_storage - _start; }
		void clear()
		{
			_finish = _start;
		}
		T& operator[](size_t pos) 
		{
			assert(pos < size());
			return _start[pos];
		}
		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}
		bool empty() const
		{
			return _start == _finish;
		}
		void reserve(size_t n) 
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (int i = 0; i < sz; i++)
						tmp[i] = std::move(_start[i]);
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}
		void resize(size_t n, const T& val = T()) 
		{
			if (n > size())
			{
				reserve(n);
				for (int i = size(); i < n; i++)
					push_back(val);
			}
			else
			{
				_finish = _start + n;
			}
		}
		void push_back(const T& val) 
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finish = val;
			_finish++;
		}
		void pop_back() 
		{
			assert(!empty());
			--_finish;
		}
		iterator insert(iterator pos, const T& val) 
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}
			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos) 
		{
			assert(pos >= _start && pos < _finish);

			iterator end = pos + 1;
			while (end < _finish)
			{
				*(end - 1) = *end;
				end++;
			}
			--_finish;
			return pos;

		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	void testVector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		for (int i = 0; i < v.size(); i++)
			std::cout << v[i] << " ";
		std::cout << std::endl;

		vector<int> v1(v);
		v1.pop_back();
		v1.push_back(10);
		v1.push_back(20);
		v1.push_back(30);

		vector<int>::iterator it = v1.begin();
		while (it != v1.end())
		{
			std::cout << *it << " ";
			++it;
		}

		v = v1;
		for (const auto& e : v)
			std::cout << e << " ";
		std::cout << std::endl;


	}
	void testVector2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.insert(v.begin(), 0);
		v.insert(v.begin() + 2, 20);
		for (auto& e : v)
			std::cout << e << " ";
		std::cout << std::endl;

		v.erase(v.begin());
		v.erase(v.end() - 1);
		for (auto& e : v)
			std::cout << e << " ";
		std::cout << std::endl;

		v.resize(10, 10);
		for (auto& e : v)
			std::cout << e << " ";
		std::cout << std::endl;

		v.resize(1);
		for (auto& e : v)
			std::cout << e << " ";
		std::cout << std::endl;
	}
	void testVector3()
	{
		vector<string> strs;
		strs.push_back("11111111111111111111");
		strs.push_back("111112342342411111");
		strs.push_back("11111145646456111111");
		strs.push_back("1111116788888811111");
		
		strs.push_back("absc");

	}
}